package com.map.jsBridge

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.webkit.JavascriptInterface
import com.google.gson.Gson
import com.map.interfaces.MapControl

class JsFun(private val mapControl: MapControl) : Any() {
    private val mapLoadComplete = 100
    private val mapClick = 101
    private val getAreaPoints = 102
    private val getArea = 103
    private val polygonClick = 104
    private val infoWindowClick = 105

    private var points: Points? = null
    private var polygonArea: String? = null
    private var polygonTag: String? = null
    private var infoWindowTag: String? = null

    //js调用java方法，是在一个javaBridge的线程执行的，所以切换下线程
    private val handler = object : Handler(Looper.myLooper()!!) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                mapLoadComplete -> mapControl.onMapLoadComplete.onComplete()
                mapClick -> {
                    val lng = msg.data.getString("lng")?.toDouble()
                    val lat = msg.data.getString("lat")?.toDouble()
                    if (lng != null && lat != null) {
                        mapControl.onMapClick.onClick(lng, lat)
                    }
                }
                getAreaPoints -> {
                    points?.let {
                        if (it.size > 0) {
                            mapControl.getAreaPoints.points(it[0])
                        }
                    }
                    points = null
                }
                getArea -> {
                    polygonArea?.let {
                        mapControl.getArea.area(it)
                    }
                    polygonArea = null
                }
                polygonClick -> {
                    polygonTag?.let {
                        mapControl.onPolygonClick.onClick(it)
                    }
                    polygonTag = null
                }
                infoWindowClick -> {
                    infoWindowTag?.let {
                        mapControl.onInfoWindowClick.onClick(it)
                    }
                }
            }
        }
    }

    /**
     * 地图加载完成
     */
    @JavascriptInterface
    fun onMapLoadComplete() {
        handler.sendEmptyMessage(mapLoadComplete)
    }

    /**
     * 地图点击事件
     */
    @JavascriptInterface
    fun onMapClick(lng: String, lat: String) {
        val bundle = Bundle().apply {
            putString("lng", lng)
            putString("lat", lat)
        }
        val msg = Message().apply {
            what = mapClick
            data = bundle
        }
        handler.sendMessage(msg)
    }

    /**
     * 获取最后一个面的所有点
     */
    @JavascriptInterface
    fun setAreaPoints(json: String) {
        //[[{"lat":30.7715,"lng":104.523},{"lat":30.7624,"lng":104.523},{"lat":30.7612,"lng":104.518},{"lat":30.7683,"lng":104.517}]]
        points = Gson().fromJson(json, Points::class.java)
        handler.sendEmptyMessage(getAreaPoints)

    }

    /**
     * 获取平面面积的回调
     */
    @JavascriptInterface
    fun getArea(area: String) {
        polygonArea = area
        handler.sendEmptyMessage(getArea)
    }

    /**
     * 地图Polygon覆盖物点金事件回调
     */
    @JavascriptInterface
    fun polygonClick(tag: String) {
        polygonTag = tag
        handler.sendEmptyMessage(polygonClick)
    }

    /**
     * 地图infoWindow是的点击效果
     */
    @JavascriptInterface
    fun infoWindowClick(tag: String) {
        infoWindowTag = tag
        handler.sendEmptyMessage(infoWindowClick)
    }
}